import 'package:boss/models/commodity.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/widget/scan_icon.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/provider/package_detail_provider.dart';
import 'package:boss/ui/page/packageGroup/picker_package_group_page.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/picker/commodityPicker/commodity_picker.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'mustCommodity/must_commodity_list.dart';
import 'packageGroup/package_group_list.dart';
import 'package_panel.dart';
import 'title_label_btn.dart';

class PackageCommodityPanel extends StatefulWidget {
  @override
  _PackageCommodityPanelState createState() => _PackageCommodityPanelState();
}

class _PackageCommodityPanelState extends State<PackageCommodityPanel> {
  TextEditingController _barCodeController;

  @override
  void initState() {
    super.initState();

    _barCodeController = TextEditingController(
      text: Provider.of<PackageDetailProvider>(context, listen: false).package.commodityBarCode,
    );
  }

  @override
  void dispose() {
    _barCodeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<PackageDetailProvider>(
      builder: (context, provider, child) {
        return PackagePanel(
          title: '套餐菜品',
          children: <Widget>[
            TitleLabelBtn(
              title: '本套餐必选的菜品',
              label: '请选择菜品',
              onTap: () async {
                var selected = provider.package.mustCommodityList
                    .map((item) => Commodity.fromJson({
                          'id': item.commodityId,
                          'sku': {
                            'skuId': item.skuId,
                          },
                          'count': item.quantity,
                        }))
                    .toList();

                var res = await showCommodityPicker(
                  context,
                  type: 1,
                  selectedCommodities: selected,
                  isShowPackage: false,
                  isShowSku: true,
                );

                if (res != null) {
                  provider.setPackageMustCommodity(res);
                }
              },
            ),
            MustCommodityList(),
            TitleLabelBtn(
              title: '本套餐顾客自选菜品',
              label: '请选择套餐分组',
              onTap: () async {
                var res = await pickerPackageGroup(
                  context,
                  selectedPackageGroups: provider.package.packagesGroupList,
                );

                if (res != null) {
                  provider.setPackageGroup(res);
                }
              },
            ),
            PackageGroupList(),
            BaseTextFormField(
              prefixText: '商品条形码',
              hintText: '选填，可填写条形码或扫码',
              controller: _barCodeController,
              suffix: ScanIcon(
                onScan: (value) {
                  _barCodeController.text = value;
                  provider.setPackageGroupBarCode(value);
                },
              ),
              onChanged: (value) {
                provider.setPackageGroupBarCode(value);
              },
            ),
          ],
        );
      },
    );
  }
}
